/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client.dialog;

import org.ajiaojr.enigmagm.client.data.Item;
import org.ajiaojr.enigmagm.client.service.ItemService;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author ajiaojr
 */
public class ItemDetailsDialog extends DialogBox {
  private static final ItemDetailsDialogMessages MESSAGES =
      (ItemDetailsDialogMessages) GWT.create(ItemDetailsDialogMessages.class);

  private Frame frame;

  private Item item;

  public ItemDetailsDialog(int itemID) {
    super(true);
    init();
    ItemService.Util.getInstance().findByItemID(itemID, new AsyncCallback() {

      public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());
      }

      public void onSuccess(Object result) {
        item = (Item) result;
        showContents();
      }
    });

  }

  public ItemDetailsDialog(Item item) {
    super(true);
    this.item = item;
    init();
  }

  private void init() {
    final VerticalPanel verticalPanel = new VerticalPanel();
    setWidget(verticalPanel);
    verticalPanel.setSize("400px", "350px");

    frame = new Frame();
    verticalPanel.add(frame);
    frame.setWidth("100%");
    frame.setHeight("100%");

    setPopupPosition((Window.getClientWidth() - 400) / 2, (Window
        .getClientHeight() - 350) / 2);
  }

  public void show() {
    showContents();
    super.show();
  }

  /**
   * 
   */
  private void showContents() {
    if (item != null) {
      setHTML(MESSAGES.title(item.getItemName()));
      frame.setUrl("http://wow.allakhazam.com/ihtml?" + item.getItemName());
    }
  }
}
